package cn.wolfcode.service.impl;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import cn.wolfcode.domain.Appointment;
import cn.wolfcode.domain.Consumption;
import cn.wolfcode.mapper.BussinessReportMapper;
import cn.wolfcode.mapper.ConsumptionMapper;
import cn.wolfcode.meiju.AppointmentStatusEnum;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IAppointmentService;
import cn.wolfcode.service.IConsumptionService;
import cn.wolfcode.util.UserContext;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.context.annotation.ApplicationScope;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Service
public class ConsumptionServiceImpl implements IConsumptionService {

    @Autowired
    private ConsumptionMapper consumptionMapper;


    @Override
    public void save(Consumption consumption) {
        consumptionMapper.insert(consumption);
    }

    @Override
    public void delete(Long id) {
        consumptionMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void update(Consumption consumption) {
        consumptionMapper.updateByPrimaryKey(consumption);
    }

    @Override
    public Consumption get(Long id) {
           return consumptionMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Consumption> listAll() {
        return consumptionMapper.selectAll();
    }

    @Override
    public PageInfo<Consumption> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize()); //对下一句sql进行自动分页
        List<Consumption> consumptions = consumptionMapper.selectForList(qo); //里面不需要自己写limit
        return new PageInfo<Consumption>(consumptions);
    }


    /**
     * 当用户到店，且进行消费时，我们要将预约信息存入到结算单中，然后更改状态为消费中
        预约单和结算单通过流水账号进行关联
        1.通过页面的预约单的流水号，找到预约单的信息，和结算单的信息，2.将预约单的信息赋值给结算单
       将结算单的部分自动创建信息给补全
     */

    @Autowired
    private IAppointmentService appointmentService;
    @Override
    public Consumption save(String appointmentAno) {
        //根据流水号找到预约单的信息
        Appointment appointment = appointmentService.selectByAno(appointmentAno);
//        appointmentService.updateStatus(appointment.getId(), AppointmentStatusEnum.CONSUME.getValue());
        appointmentService.updateStatus(appointment.getId(), AppointmentStatusEnum.CONSUME.getValue());
        //新建一个结算单
        Consumption consumption = new Consumption();
        //将预约单的信息赋值给结算单
        //将结算单赋值给结算单（这两个单就是通过流水号进行关联的）
        consumption.setAppointmentAno(appointmentAno);   //设值关联预约单
        consumption.setStatus(0);               //设值状态
        consumption.setCustomerTel(appointment.getContactTel());  //设值用户联系方式
        consumption.setCustomerName(appointment.getContactName()); //设值用户姓名
        //从session中取出登录后的用户，为操作人员，即是结算人
        consumption.setCreateUserId(UserContext.getCurrentEmployee());  //设值当前结算人
        consumption.setCheckinTime(new Date());                    //设值进店时间
        consumption.setBusinessId(appointment.getBusiness());    //设值门店
        consumption.setCreateTime(new Date());  //设值创建时间

        String dateStr = DateUtil.format(new Date(), "yyyyMMddHHmm");
        String numbers = RandomUtil.randomNumbers(5);
        consumption.setCno(dateStr+numbers);  //设值订单的流水号

        consumptionMapper.insert(consumption);
        System.out.println(consumption);
        return consumption;

    }

    @Autowired
    private BussinessReportMapper bussinessReportMapper;
    @Override
    public PageInfo<HashMap> selectByBussiness(QueryObject queryObject) {
        PageHelper.startPage(queryObject.getCurrentPage(), queryObject.getPageSize());
        List<HashMap> maps = bussinessReportMapper.selectByBussiness(queryObject);
        return new PageInfo<>(maps);
    }
}
